﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Moq;

namespace Opo.Perspective.Core
{
    [TestFixture]
    public class DataServiceTests
    {
        IRepository<IPerspectiveEntity> _repository;
        List<IPerspectiveEntity> _entities;
        IPerspectiveEntity _firstEntity;
        Guid _firstID;

        [TestFixtureSetUp]
        public void Setup()
        {
        }

        [TestFixtureTearDown]
        public void Teardown()
        {

        }

        [SetUp]
        public void Init()
        {
            _repository = new RepositoryStub();
            _entities = new List<IPerspectiveEntity>(_repository.GetAll());
            _firstEntity = _entities[0];
            _firstID = _entities[0].ID;
        }

        [Test]
        public void CanGetEntities()
        {
            IService<IPerspectiveEntity> service = new DataService<IPerspectiveEntity>(_repository);

            Assert.That(service.GetAll().Count, Is.EqualTo(new List<IPerspectiveEntity>(_repository.GetAll()).Count));
            Assert.That(service.Get(_firstID), Is.EqualTo(_repository.Get(_firstID)));
        }

        [Test]
        public void CanUpdateEntity()
        {
            IService<IPerspectiveEntity> service = new DataService<IPerspectiveEntity>(_repository);
            int numberOfEntities = new List<IPerspectiveEntity>(service.GetAll()).Count;

            DateTime date = new DateTime(2000, 1, 1, 0, 0, 0);
            string title = "New Title";
            Flag flag = new Flag("New Flag");
            TagCollection tags = new TagCollection() { new Tag("New Tag 1"), new Tag("New Tag 2") };
            IPerspectiveEntity entity = new EntityStub()
            {
                ID = _firstID,
                Date = date,
                Flag = flag,
                IsRead = true,
                Tags = tags,
                Title = title
            };
            service.Update(entity);

            IPerspectiveEntity firstEntity = new List<IPerspectiveEntity>(service.GetAll())[0];
            Assert.That(firstEntity.Date, Is.EqualTo(date));
            Assert.That(firstEntity.Flag.Name, Is.EqualTo(flag.Name));
            Assert.That(firstEntity.IsRead, Is.True);
            Assert.That(firstEntity.Tags[0], Is.EqualTo(tags[0]));
            Assert.That(firstEntity.Title, Is.EqualTo(title));
        }

        [Test]
        public void CanAddEntities()
        {
            IService<IPerspectiveEntity> service = new DataService<IPerspectiveEntity>(_repository);
            int numberOfEntities = new List<IPerspectiveEntity>(service.GetAll()).Count;

            service.Add(new EntityStub());
            int newNumberOfEntities = new List<IPerspectiveEntity>(service.GetAll()).Count;

            Assert.That(newNumberOfEntities, Is.EqualTo(numberOfEntities + 1));
        }

        [Test]
        public void CanDeleteEntities()
        {
            IService<IPerspectiveEntity> service = new DataService<IPerspectiveEntity>(_repository);
            int numberOfEntities = new List<IPerspectiveEntity>(service.GetAll()).Count;

            service.Delete(_firstEntity);
            int newNumberOfEntities = new List<IPerspectiveEntity>(service.GetAll()).Count;

            Assert.That(newNumberOfEntities, Is.EqualTo(numberOfEntities - 1));
        }
    }
}
